package controlador;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FrontController extends HttpServlet {

    private String vistas[] = {"indice", "seccion", "documento", "termycondi", "usuario", "reportes", "actividad"};
    private String acciones[] = {"nuevo", "eliminar", "editar", "listado", "login", "configurar"
            , "reporte","menu", "calendario", "inspecciones", "avanzada", "detalle", "portabilidad"};

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * v: 0.-index 1.-seccion 2.-documento 3.-termiycondi 4.-usuario 5.-reportes
     * ac: 0.-nuevo 1.-eliminar 2.-editar 3.-listado 4.- login 5.- configurar 
     * 6.-reporte 7.- menu 8.- calendario 9.- inspecciones 10.- avanzada 
     * 11.- detalle 12. portabilidad
     * 
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=ISO-8859-1");
        PrintWriter out = response.getWriter();
        if (request.getSession().getAttribute("sUsuario") == null) {
            request.getSession().invalidate();
            response.sendRedirect(getServletContext().getContextPath());
        }
        try {
            int vista, accion;
            if (request.getSession().getAttribute("sUsuario") == null) {
                vista = 4;
                accion = 4;
            } else {
                vista = request.getParameter("v") == null ? -1 : Integer.parseInt(request.getParameter("v"));
                accion = request.getParameter("ac") == null ? -1 : Integer.parseInt(request.getParameter("ac"));
            }
            String url = "/jsp/" + vistas[vista] + "/" + acciones[accion] + ".jsp";
            out.println(url);
            getServletContext().getRequestDispatcher(url).forward(request, response);
        } catch (Exception e) {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Error - Front Controller</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("Error al cargar la pagina solicitada");
            out.println("<br/><br/><br/>" + e.toString() + "<br/>" + e.getMessage());
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
